package com.ksd.modules.members.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.members.entity.AdminUserVipCode;
import com.ksd.modules.members.mapper.AdminUserVipCodeMapper;
import com.ksd.modules.members.po.AdminUserVipCodeSavePO;
import com.ksd.modules.members.po.AdminUserVipCodeSearchPO;
import com.ksd.modules.members.service.IAdminUserVipCodeService;
import com.ksd.modules.members.vo.AdminUserVipCodeVo;
import com.ksd.utils.snow.SnowflakeIdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminUserVipCodeServiceImpl extends BaseServiceImpl<AdminUserVipCodeMapper, AdminUserVipCode> implements IAdminUserVipCodeService {



    /**
     * 查询列表
     * @param adminUserVipCodeSearchPO
     * @return
     */
    public Page<AdminUserVipCodeVo> findAdminUserVipCodes(AdminUserVipCodeSearchPO adminUserVipCodeSearchPO){
        Page<AdminUserVipCode> page = new Page<>(adminUserVipCodeSearchPO.getPageNo(),adminUserVipCodeSearchPO.getPageSize());
        LambdaQueryWrapper<AdminUserVipCode> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminUserVipCode::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserVipCodeSearchPO.getVipId()), AdminUserVipCode::getVipId,adminUserVipCodeSearchPO.getVipId());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminUserVipCodeSearchPO.getCode()), AdminUserVipCode::getCode,adminUserVipCodeSearchPO.getCode());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserVipCodeSearchPO.getMark()), AdminUserVipCode::getMark,adminUserVipCodeSearchPO.getMark());
        lambdaQueryWrapper.orderByAsc(AdminUserVipCode::getMark);
        Page<AdminUserVipCode> adminUserVipCodePage = this.page(page, lambdaQueryWrapper);
        Page<AdminUserVipCodeVo> adminUserVipCodeVoPage = tranferPageBo(adminUserVipCodePage, AdminUserVipCodeVo.class);
        return adminUserVipCodeVoPage;
    }


    /**
     * 批量创建3张兑换码
     * @param adminUserVipCodeSavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean createBatchCode(AdminUserVipCodeSavePO adminUserVipCodeSavePO){
        AdminUserVipCode adminUserVipCode = tranferBo(adminUserVipCodeSavePO, AdminUserVipCode.class);
        adminUserVipCode.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminUserVipCode.setSystemName(SystemIdThrealLocal.get().getSystemName());
        adminUserVipCode.setCode(new SnowflakeIdWorker(9,8).nextId()+"");
        adminUserVipCode.setMark(0);
        this.saveOrUpdate(adminUserVipCode);
       return true;
    }

    /**
     * 根据ID获取明细
     * @param id
     * @return
     */
    public AdminUserVipCodeVo getAdminUserVipCodeById(Long id){
        AdminUserVipCode adminUserVipCode = this.getById(id);
        AdminUserVipCodeVo adminUserVipCodeVo = tranferBo(adminUserVipCode, AdminUserVipCodeVo.class);
        return adminUserVipCodeVo;
    }

    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminUserVipCodeById(Long id){
        boolean b = this.removeById(id);
        return b;
    }


    /**
     * 批量物理删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRemovesAdminUserVipCode(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }

}
